package com.maycari.captainbi.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.maycari.captainbi.common.constant.CaptainBIConstant;
import com.maycari.captainbi.common.exception.BusinessException;
import com.maycari.captainbi.data.dto.Captain1Dto;
import com.maycari.captainbi.data.dto.Captain3Dto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.service.Captain1Service;
import com.maycari.captainbi.service.Captain3Service;
import com.maycari.captainbi.util.CaptainBIUtil;
import com.maycari.captainbi.util.MaycariRedis;
import com.maycari.tool.utils.BeanUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 王宏伟
 * @DateTime: 2021/4/1 11:36
 * @Description: 获取FBA销售报告列表
 */
@Component
public class Task1 implements ApplicationRunner {

    @Autowired
    private MaycariRedis maycariRedis;

    private static String start_time_str = "2021-03-01 00:00:00";

    private static String dataKey = "MAYCARI:DATAKEY1";

    private static String action = "http://cronapi.captainbi.com/index.php?m=cronapi&c=goods&a=getAmazonGoodsList";

    @Autowired
    private Captain1Service captain1Service;

    @Autowired
    private JmsTemplate jmsTemplate;

    @PostMapping
    public void task() {
        jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER,"1");
    }

    private void getData() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        int page = 1;
        int page_size = 100;

        Date start_time = maycariRedis.get(dataKey);
        if (start_time == null) {
            start_time = DateUtil.parse(start_time_str, DateUtil.PATTERN_DATETIME);
        }

        Date now = new Date();

        while (true) {
            JSONArray jsonArray = new JSONArray();
            //一天
            Date end_time = DateUtil.minusSeconds(DateUtil.plusDays(start_time, 1), 1);

            if (end_time.after(now)) {
                break;
            }
            while (true) {
                ThreadUtil.sleep(1000);
                TreeMap<String, String> params = new TreeMap<>();
                params.put("page", String.valueOf(page));
                params.put("page_size", String.valueOf(page_size));
                params.put("start_time", simpleDateFormat.format(start_time));
                params.put("end_time", simpleDateFormat.format(end_time));
                String result = CaptainBIUtil.sendRequest(CaptainBIConstant.CaptainBIUrl.url_1.value(), params);
                JSONObject jsonObject = JSON.parseObject(result, Feature.OrderedField);
                int code = jsonObject.getIntValue("code");
                if (code == 200) {
                    JSONArray data = jsonObject.getJSONArray("data");
                    if (data != null && data.size() > 0) {
                        if (data.size() >= 100) {
                            //可能还有数据  继续请求
                            jsonArray.addAll(data);
                            page++;
                        } else {
                            jsonArray.addAll(data);
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    throw new BusinessException(String.format("Task1 error :【%s】", jsonObject.getString("msg")));
                }
            }
            start_time = DateUtil.plusSeconds(end_time, 1);
            List<Captain1Dto> collect = jsonArray.toJavaList(Captain1Dto.class);
            //captain1Service.insertBatch(collect);
            maycariRedis.set(dataKey, start_time);
        }

    }

    @Override
    public void run(ApplicationArguments args) {
        //getData();
    }


}

